package com.demo.delivery;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ParseException;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class PendingOrdersActivity extends ListActivity implements FetchOrdersListner {

	private ProgressDialog dialog;
	boolean firstRun = true;
	private SharedPreferences preferences = null;

	OrdersAdapter adapter = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_orders);
		Intent i = getIntent();

		String user = i.getStringExtra("user");

		Date d = new Date();
		String dateWithoutTime = d.toString().substring(0, 10);

		setTitle("Pending Orders for " + dateWithoutTime);

		initOrdersView(user);

	}

	public void initOrdersView(String Customer) {
		firstRun = false;

		// show progress dialog
		dialog = ProgressDialog.show(this, "", "Loading...");

		String url = "http://qrcode.educroc.com/order_zones";
		FetchOrdersTask task = new FetchOrdersTask(this);
		task.execute(url);
	}

	public void onCallClicked(View v) {
		int position = (int) v.getTag();

		String tel = adapter.getOrder(position).getMobile();

		Intent intent = new Intent(Intent.ACTION_DIAL);
		intent.setData(Uri.parse(String.format("tel:%s", tel)));
		startActivity(intent);

	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);

		Order selectedOrder = (Order) l.getItemAtPosition(position);
		Intent intent = new Intent(this, OrderActivity.class);

		intent.putExtra("card_no", selectedOrder.getCard_no());
		intent.putExtra("client_name", selectedOrder.getClient_name());
		intent.putExtra("address", selectedOrder.getAddress());
		intent.putExtra("mobile", selectedOrder.getMobile());
		intent.putExtra("breakfast_quantity",
				selectedOrder.getBreakfast_quantity());
		intent.putExtra("lunch_quantity", selectedOrder.getLunch_quantity());
		intent.putExtra("dinner_quantity", selectedOrder.getDinner_quantity());
		intent.putExtra("amount", selectedOrder.getAmount());
		intent.putExtra("id", selectedOrder.getId());

		startActivity(intent);

	}

	@Override
	public void onFetchFailure(String msg) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		// show failure message
		Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
	}

	@Override
	public void onFetchOrdersComplete(List<Order> data) {
		// dismiss the progress dialog
		if (dialog != null)
			dialog.dismiss();
		List<Order> pendingOrders = new ArrayList<Order>();
		for(Order order:data)
		{
			String collection = order.getCollection();
			if((collection)== "null")
			{
				pendingOrders.add(order);
			}
		}
		// create new adapter
		adapter = new OrdersAdapter(this, pendingOrders);
		// set the adapter to list
		setListAdapter(adapter);

	}

}